var xmlHttp;
var replaceData;
var cursor;
var tooltipOn;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        if (window.XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        }
    }
}
function getPopupDiv() {
    return document.getElementById("popup");
}
function getReplaceSpan() {
    return document.getElementById("replacePopupData");
}
function startTooltip(event, url) {
    replace = getReplaceSpan();
    tooltipOn = true;
    if (replaceData == null) {
        replaceData = replace.innerHTML;
    }
    if (typeof (event) == "undefined") {
        event = window.event;
    }
    var element = getPopupDiv();
    cursor = getPosition(event);
    fitInsideOfScreenGracefully(element);
    createXMLHttpRequest();
    xmlHttp.open("GET", url, true);
    xmlHttp.onreadystatechange = callback;
    xmlHttp.send(null);
}
function callback() {
    try {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            setTooltip(xmlHttp.responseText);
        }
    }
    catch (e) {//failed... do nothing
    }
}
function setTooltip(data) {
    replace = getReplaceSpan();
    replace.innerHTML = data;
    fitInsideOfScreenGracefully(getPopupDiv());
}
function fitInsideOfScreenGracefully(element) {
    if (tooltipOn) {

        var frame = getWindowFrame();

        var height = element.offsetHeight;
        var width = element.offsetWidth;
        var overlapCursor = false;

        var offset = 25;
        //var position = "";

        element.style.right = "auto";


        if (height > frame.height) {
            //element is bigger than height of window,
            //put it at the top of the visibile window (up)
            element.style.top = frame.y + "px";
            overlapCursor = true;
            //position += "1,";
        } else {
            if (height + cursor.y + offset > frame.height) {
                //element is bigger than can fit below the cursor
                if (height < cursor.y - offset) {
                    //element can fit above cursor
                    element.style.top = (cursor.y - height - offset + frame.y) + "px";
                    //position += "2,";

                } else {
                    //element can fit to the side of the cursor just fine
                    element.style.top = (frame.height - height + frame.y) + "px";
                    overlapCursor = true;
                    //position += "3,";

                }
            } else {
                //element will fit below the cursor
                element.style.top = (cursor.y + offset + frame.y) + "px";
                //position += "4,";
            }
        }


        if (!overlapCursor && cursor.x + width + offset > frame.width)
        {
            //element can fit below cursor
            if (width + offset < cursor.x)
            {
                //and it can fit all the way to the left of the cursor
                element.style.left = (cursor.x + frame.x - offset - width) + "px";
                //position += "5";
            }
            else if (width + offset < frame.width)
            {
                //put it as right as possible
                element.style.left = (frame.width - width - offset) + "px";
                //position += "6";
            }
            else
            {
                //otherwise just stick it as far to the left as possible.
                element.style.left = frame.x + "px";
                //position += "7";
            }
        }
        else if (overlapCursor && width + offset < cursor.x && frame.width / 2 < cursor.x)
        {
            //if the element will overlap if given the chance, and there is more room
            //on the left, put it over there, with a left = 'auto'
            element.style.left = "auto";
            element.style.right = (frame.width - cursor.x + offset - frame.x) + "px";
            //position += "8";
        }
        else {
            //element can (or has to) fit to the right of the cursor
            element.style.left = (cursor.x + offset + frame.x) + "px";
            //position += "10";
        }

        element.style.display = "block";

        //return position;
    }
}

function getWindowFrame() {
    var frame = {width:0, height:0, x:0, y:0};
    if (typeof (window.innerWidth) == "number") {
        //Non-IE
        frame.width = window.innerWidth;
        frame.height = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        frame.width = document.documentElement.clientWidth;
        frame.height = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        frame.width = document.body.clientWidth;
        frame.height = document.body.clientHeight;
    }

    if (typeof (window.pageYOffset) == "number") {
        //Netscape compliant
        frame.y = window.pageYOffset;
        frame.x = window.pageXOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        frame.y = document.body.scrollTop;
        frame.x = document.body.scrollLeft;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        frame.y = document.documentElement.scrollTop;
        frame.x = document.documentElement.scrollLeft;
    }

    //crop a little for window scroll bars
    frame.width = frame.width - 20;
    frame.height = frame.height - 20;
    return frame;
}

function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.clientX || e.clientY) {
        cursor.x = e.clientX;
        cursor.y = e.clientY;
    }
    return cursor;
}
function stopTooltip() {
    tooltipOn = false;
    getPopupDiv().style.display = "none";
    replace = getReplaceSpan();
    replace.innerHTML = replaceData;
}
	
